<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>

      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="doSearch">查询</el-button>

    </div>

    <div class="create-container">
      <el-button type="primary" @click="HandleAdd">添加员工</el-button>

    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" :index="getIndex" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ scope.row.status===1?'启用':'禁用' }}
          </template>

        </el-table-column>

        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" @click="HandleEdit(scope.row.id)">编辑</el-button>

            <el-button size="mini" type="text" @click="handleDelete(scope.row.id)">删除</el-button>

            <el-button size="mini" type="text">重置密码</el-button>

          </template>

        </el-table-column>

      </el-table>

    </div>

    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :current-page="params.page"
        :page-size="params.pageSize"
        @current-change="getPageValue"
      />
    </div>

    <!-- 添加员工 -->
    <el-dialog
      :title="ids === 0 ? '编辑员工' : '添加员工'"
      :visible.sync="dialogVisible"
      width="480px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>

          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>

          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>

          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>

          </el-form-item>

          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>

              <el-radio :label="1">启用</el-radio>

            </el-radio-group>

          </el-form-item>

        </el-form>

      </div>

      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>

        <el-button size="mini" type="primary" @click="confirmSubmit">确 定</el-button>

      </template>

    </el-dialog>
  </div>

</template>

<script>
import { getEmployList, deleteEmploy, addEmploy, getRoleList, getEmployById, updateEmploy } from '@/api/employ'
// rule规则
// value检查的数据 true/false
// callback 函数 执行这个函数
// 成功执行callback 失败也执行callback(错误对象 new Error(错误信息))
const validatePhone = (rule, value, callback) => {
  if (/^1[3-9]\d{9}$/.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的手机号'))
  }
}
export default {
  name: 'Employee',
  data() {
    return {
      total: 0,
      ids: 0,

      params: {
        page: 1,
        pageSize: 5,
        name: ''
      },
      employeeList: [], // 员工列表
      dialogVisible: false, // 控制弹框打开关闭
      roleList: [], // 角色列表
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      roleName: '', // 角色名称
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployList()
  },
  methods: {
    // 获取员工列表
    async getEmployList() {
      const res = await getEmployList(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
      this.roleName = res.data.rows.roleName
    },
    // 分页
    getPageValue(currentPage) {
      this.params.page = currentPage
      this.getEmployList()
    },
    // 翻页时序号连续
    getIndex(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 搜索
    doSearch() {
      this.params.page = 1
      this.getEmployList()
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该员工信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteEmploy(id)
        this.getEmployList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleList()

      this.roleList = res.data
    },
    // 添加
    HandleAdd(ids = 1) {
      this.ids = ids
      this.dialogVisible = true
      this.getRoleList()
    },
    // 编辑
    async HandleEdit(id) {
      this.dialogVisible = true
      this.ids = 0

      const res = await getEmployById(id)
      this.getRoleList()
      this.addForm = res.data

      delete this.addForm.createTime
      console.log(this.addForm)
    },

    confirmSubmit() {
      this.$refs.addForm.validate(async(result) => {
        if (result) {
          if (this.addForm.id) {
            await updateEmploy(this.addForm)
          } else {
            const res = await addEmploy(this.addForm)
            console.log(res)
          }

          this.getEmployList()
          this.dialogVisible = false
          this.addForm = {}
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
